﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;

namespace WpfAdvControls.AssemblyStarter
{
    /// <summary>
    ///     Interaction logic for AssemblyStartersView.xaml
    /// </summary>
    public partial class AssemblyStartersView : UserControl
    {
        private Assembly assembly;
        private readonly AssemblyStartersContext context;
        private Type currentType;

        public AssemblyStartersView()
        {
            InitializeComponent();
            context = AssemblyStartersContext.GetCurrentStartersContext();
            assembliesGrid.DataContext = context;
            assembliesGrid.SetBinding(ItemsControl.ItemsSourceProperty, "Starters");
        }

        private void OpenFileClick(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == true)
            {
                assemblyFile.Text = dialog.FileName;
            }
        }

        private void AssemblyFileTextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                assembly = Assembly.LoadFrom(assemblyFile.Text);
                var types = assembly.GetTypes().Where((Type type) => type.IsClass);
                className.ItemsSource = types;
            }
            catch
            {
                // To Do Error Message
            }
        }

        private void ClassNameSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            currentType = className.SelectedItem as Type;
            if (currentType != null)
            {
                var methods = currentType.GetMethods();
                methodName.ItemsSource = methods;
            }
        }

        private void AddAssemblyStarter(object sender, RoutedEventArgs e)
        {
            var methodInfo = methodName.SelectionBoxItem as MethodInfo;
            if (methodInfo == null) return;
            context.Starters.Add(new AssemblyStarter
            {
                AssemblyFile = assemblyFile.Text,
                ClassName = className.Text,
                MethodName = methodInfo.Name
            });
            AssemblyStartersContext.Settings.SaveSettings();
        }

        private void StartClick(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                var assemblyStarter = btn.Tag as AssemblyStarter;
                if (assemblyStarter != null)
                {
                    assemblyStarter.Start();
                }
            }
        }

        private void DeleteClick(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                var assemblyStarter = btn.Tag as AssemblyStarter;
                if (assemblyStarter != null)
                {
                    context.Starters.Remove(assemblyStarter);
                }
            }
        }
    }
}